package com.zhongwang.mms.module.half.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.module.half.dao.HOrderDetailDao;
import com.zhongwang.mms.module.half.model.HOrderDetailModel;
import com.zhongwang.mms.module.half.model.HalInProReportExport;
import com.zhongwang.mms.module.half.service.HBasicService;
import com.zhongwang.mms.module.half.service.HReportFormsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Description: 成品库统计报表 控制层
 * @Author: th
 * @Date: 2020/06/09
 */

@Controller
@RequestMapping("/half/reportForms")
public class HReportFormsController extends BaseController {

    @Autowired
    private HBasicService hBasicService;
    @Autowired
    private HReportFormsService hReportFormsService;
    @Autowired
    private HOrderDetailDao hOrderDetailDao;

    /**
     * 首页图标跳转
     * @param model
     * @param type
     * @return
     */
    @GetMapping("/list/{type}")
    public String lists(Model model, @PathVariable("type") Integer type){
       // Date date = new Date();
        Calendar ct = Calendar.getInstance();
        // 时
        ct.set(Calendar.HOUR_OF_DAY, 0);
        // 分
        ct.set(Calendar.MINUTE, 0);
        // 秒
        ct.set(Calendar.SECOND, 0);
        // 毫秒
        ct.set(Calendar.MILLISECOND, 0);

        Date time = ct.getTime();
        ct.setTime(time);
        ct.set(Calendar.DAY_OF_MONTH, 1);
        //月第一天
        Date startDate = ct.getTime();
        ct.set(Calendar.DAY_OF_MONTH, ct.getActualMaximum(Calendar.DAY_OF_MONTH));
        //月最后一天
        Date endDate = ct.getTime();
        Calendar currCal=Calendar.getInstance();
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        //年第一天
        calendar.set(Calendar.YEAR,currCal.get(Calendar.YEAR));
        Date st = calendar.getTime();
        calendar.clear();
        //年最后一天
        calendar.set(Calendar.YEAR,currCal.get(Calendar.YEAR));
        calendar.roll(Calendar.DAY_OF_YEAR,-1);
        Date et = calendar.getTime();
        HOrderDetailModel hOrderDetailModel = new HOrderDetailModel();
        if(type == 1){//整体外协月入库明细及金额
            hOrderDetailModel.setOdaMatType(2);
            hOrderDetailModel.setOdrType(1);
            hOrderDetailModel.setStartStorageDate(startDate);
            hOrderDetailModel.setEndStorageDate(endDate);
            HOrderDetailModel hOrderDetail = hOrderDetailDao.selectWeightAmount(hOrderDetailModel);
            if (hOrderDetail == null){
                model.addAttribute("amount",0);
            }else {
                model.addAttribute("amount",hOrderDetail.getOdaAmount());
            }
            model.addAttribute("odaMatType",2);//外协件
            model.addAttribute("odrType",1);//入库
            model.addAttribute("startDate",startDate);
            model.addAttribute("tittle",1);
            model.addAttribute("endDate",endDate);
            model.addAttribute("warInfo", hBasicService.getFinishWarInfo());
            model.addAttribute("dept", hBasicService.getDept());
            return "reportForms/halMonth/list";
        }else if(type == 2){
            hOrderDetailModel.setOdaMatType(2);
            hOrderDetailModel.setOdrType(2);
            hOrderDetailModel.setStartStorageDate(startDate);
            hOrderDetailModel.setEndStorageDate(endDate);
            HOrderDetailModel hOrderDetail = hOrderDetailDao.selectWeightAmount(hOrderDetailModel);
            if (hOrderDetail == null){
                model.addAttribute("amount",0);
            }else {
                model.addAttribute("amount",hOrderDetail.getOdaAmount());
            }
            model.addAttribute("odaMatType",2);//外协件
            model.addAttribute("odrType",2);//出库
            model.addAttribute("startDate",startDate);
            model.addAttribute("tittle",2);
            model.addAttribute("endDate",endDate);
            model.addAttribute("warInfo", hBasicService.getFinishWarInfo());
            model.addAttribute("dept", hBasicService.getDept());
            return "reportForms/halMonth/list";
        }else if(type == 3){
            hOrderDetailModel.setOdaMatType(2);
            hOrderDetailModel.setOdrType(1);
            hOrderDetailModel.setStartStorageDate(st);
            hOrderDetailModel.setEndStorageDate(et);
            HOrderDetailModel hOrderDetail = hOrderDetailDao.selectWeightAmount(hOrderDetailModel);
            if (hOrderDetail == null){
                model.addAttribute("amount",0);
            }else {
                model.addAttribute("amount",hOrderDetail.getOdaAmount());
            }
            model.addAttribute("odaMatType",2);//外协件
            model.addAttribute("odrType",1);//入库
            model.addAttribute("startDate",st);
            model.addAttribute("tittle",3);
            model.addAttribute("endDate",et);
            model.addAttribute("warInfo", hBasicService.getFinishWarInfo());
            model.addAttribute("dept", hBasicService.getDept());
            return "reportForms/halMonth/list";
        }else if(type == 4){
            hOrderDetailModel.setOdaMatType(2);
            hOrderDetailModel.setOdrType(2);
            hOrderDetailModel.setStartStorageDate(st);
            hOrderDetailModel.setEndStorageDate(et);
            HOrderDetailModel hOrderDetail = hOrderDetailDao.selectWeightAmount(hOrderDetailModel);
            if (hOrderDetail == null){
                model.addAttribute("amount",0);
            }else {
                model.addAttribute("amount",hOrderDetail.getOdaAmount());
            }
            model.addAttribute("odaMatType",2);//外协件
            model.addAttribute("odrType",2);//出库
            model.addAttribute("startDate",st);
            model.addAttribute("tittle",4);
            model.addAttribute("endDate",et);
            model.addAttribute("warInfo", hBasicService.getFinishWarInfo());
            model.addAttribute("dept", hBasicService.getDept());
            return "reportForms/halMonth/list";
        }else if(type == 5){
            hOrderDetailModel.setOdaMatType(4);
            hOrderDetailModel.setOdrType(1);
            hOrderDetailModel.setStartStorageDate(startDate);
            hOrderDetailModel.setEndStorageDate(endDate);
            HOrderDetailModel hOrderDetail = hOrderDetailDao.selectWeightAmount(hOrderDetailModel);
            if (hOrderDetail == null){
                model.addAttribute("amount",0);
                model.addAttribute("weight",0);
            }else {
                model.addAttribute("amount",hOrderDetail.getOdaAmount());
                model.addAttribute("weight",Math.round(hOrderDetail.getOdaWeight())/1000.0);
            }
            model.addAttribute("odaMatType",4);//铝料
            model.addAttribute("odrType",1);//入库
            model.addAttribute("startDate",startDate);
            model.addAttribute("tittle",5);
            model.addAttribute("endDate",endDate);
            model.addAttribute("warInfo", hBasicService.getFinishWarInfo());
            model.addAttribute("dept", hBasicService.getDept());
            return "reportForms/halMonth/list";
        }else if(type == 6){
            hOrderDetailModel.setOdaMatType(4);
            hOrderDetailModel.setOdrType(2);
            hOrderDetailModel.setStartStorageDate(startDate);
            hOrderDetailModel.setEndStorageDate(endDate);
            HOrderDetailModel hOrderDetail = hOrderDetailDao.selectWeightAmount(hOrderDetailModel);
            if (hOrderDetail == null){
                model.addAttribute("amount",0);
                model.addAttribute("weight",0);
            }else {
                model.addAttribute("amount",hOrderDetail.getOdaAmount());
                model.addAttribute("weight",Math.round(hOrderDetail.getOdaWeight())/1000.0);
            }
            model.addAttribute("odaMatType",4);//铝料
            model.addAttribute("odrType",2);//出库
            model.addAttribute("startDate",startDate);
            model.addAttribute("tittle",6);
            model.addAttribute("endDate",endDate);
            model.addAttribute("warInfo", hBasicService.getFinishWarInfo());
            model.addAttribute("dept", hBasicService.getDept());
            return "reportForms/halMonth/list";
        }else if(type == 7){
            hOrderDetailModel.setOdaMatType(4);
            hOrderDetailModel.setOdrType(1);
            hOrderDetailModel.setStartStorageDate(st);
            hOrderDetailModel.setEndStorageDate(et);
            HOrderDetailModel hOrderDetail = hOrderDetailDao.selectWeightAmount(hOrderDetailModel);
            if (hOrderDetail == null){
                model.addAttribute("amount",0);
                model.addAttribute("weight",0);
            }else {
                model.addAttribute("amount",hOrderDetail.getOdaAmount());
                model.addAttribute("weight",Math.round(hOrderDetail.getOdaWeight())/1000.0);
            }
            model.addAttribute("odaMatType",4);//铝料
            model.addAttribute("odrType",1);//入库
            model.addAttribute("startDate",st);
            model.addAttribute("tittle",7);
            model.addAttribute("endDate",et);
            model.addAttribute("warInfo", hBasicService.getFinishWarInfo());
            model.addAttribute("dept", hBasicService.getDept());
            return "reportForms/halMonth/list";
        }else if(type == 8){
            hOrderDetailModel.setOdaMatType(4);
            hOrderDetailModel.setOdrType(2);
            hOrderDetailModel.setStartStorageDate(st);
            hOrderDetailModel.setEndStorageDate(et);
            HOrderDetailModel hOrderDetail = hOrderDetailDao.selectWeightAmount(hOrderDetailModel);
            if (hOrderDetail == null){
                model.addAttribute("amount",0);
                model.addAttribute("weight",0);
            }else {
                model.addAttribute("amount",hOrderDetail.getOdaAmount());
                model.addAttribute("weight",Math.round(hOrderDetail.getOdaWeight())/1000.0);
            }
            model.addAttribute("odaMatType",4);//铝料
            model.addAttribute("odrType",2);//出库
            model.addAttribute("startDate",st);
            model.addAttribute("tittle",8);
            model.addAttribute("endDate",et);
            model.addAttribute("warInfo", hBasicService.getFinishWarInfo());
            model.addAttribute("dept", hBasicService.getDept());
            return "reportForms/halMonth/list";
        }else if(type == 9){
            model.addAttribute("odaMatType",3);//制作件
            model.addAttribute("odrType",1);//入库
            model.addAttribute("startDate",startDate);
            model.addAttribute("tittle",9);
            model.addAttribute("endDate",endDate);
            model.addAttribute("warInfo", hBasicService.getFinishWarInfo());
            model.addAttribute("dept", hBasicService.getDept());
            return "reportForms/halMonth/list";
        }else if(type == 10){
            model.addAttribute("odaMatType",3);//制作件
            model.addAttribute("odrType",2);//出库
            model.addAttribute("startDate",startDate);
            model.addAttribute("tittle",10);
            model.addAttribute("endDate",endDate);
            model.addAttribute("warInfo", hBasicService.getFinishWarInfo());
            model.addAttribute("dept", hBasicService.getDept());
            return "reportForms/halMonth/list";
        }else if(type == 11){
            model.addAttribute("odaMatType",3);//制作件
            model.addAttribute("odrType",1);//入库
            model.addAttribute("startDate",st);
            model.addAttribute("tittle",11);
            model.addAttribute("endDate",et);
            model.addAttribute("warInfo", hBasicService.getFinishWarInfo());
            model.addAttribute("dept", hBasicService.getDept());
            return "reportForms/halMonth/list";
        }else if(type == 12){
            model.addAttribute("odaMatType",3);//制作件
            model.addAttribute("odrType",2);//出库
            model.addAttribute("startDate",st);
            model.addAttribute("tittle",12);
            model.addAttribute("endDate",et);
            model.addAttribute("warInfo", hBasicService.getFinishWarInfo());
            model.addAttribute("dept", hBasicService.getDept());
            return "reportForms/halMonth/list";
        }else if(type == 13){
            model.addAttribute("tittle",13);
            return "reportForms/halInProReport/list";
        }
        return null;
    }


    /**
     * 制作件在库项目明细表
     * @param param
     * @return
     */
    @ResponseBody
    @PostMapping("/halInProReportList")
    public JsonResult halInProReportList(HalInProReportExport param) {
        List<HalInProReportExport> list = hReportFormsService.findHalInProReportList(param);
        return jsonResult(list);
    }

    @ResponseBody
    @GetMapping("/halInProReportExcel")
    public void halInProReportExcel(HttpServletResponse response, HalInProReportExport param) {
        hReportFormsService.halInProReportExcel(response, param);
    }

}
